package com.grad.why.studentsystem.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.grad.why.studentsystem.domain.Classinfo;
import com.grad.why.studentsystem.domain.Project;
import com.grad.why.studentsystem.domain.Student;
import com.grad.why.studentsystem.domain.current.CurrentClassinfo;
import com.grad.why.studentsystem.domain.current.PersonArriveInfoNameAndMax;
import com.grad.why.studentsystem.mapper.ClassinfoMapper;
import com.grad.why.studentsystem.mapper.GradeMapper;
import com.grad.why.studentsystem.mapper.ProjectMapper;
import com.grad.why.studentsystem.mapper.StudentMapper;
import com.grad.why.studentsystem.service.ClassesTableService;
import com.grad.why.studentsystem.service.ClassinfoService;
import com.grad.why.studentsystem.util.Util;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileInputStream;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;

@Component
@Service(interfaceClass = ClassinfoService.class)
@Transactional
public class ClassinfoServiceImpl implements ClassinfoService{
    @Autowired
    private ClassinfoMapper classinfoMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private GradeMapper gradeMapper;

    @Override
    public List<Classinfo> getClassInfos() {
       try{
            return classinfoMapper.getClassInfos();
       }catch (Exception e){
           e.printStackTrace();
       }
       return null;
    }

    @Override
    public List<CurrentClassinfo> getClassInfoList(int offset, int limit, String sort, String search) {
        try{
            return classinfoMapper.getClassInfoList(offset,limit,sort,search);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long getClassInfoListLength(String search) {
        try{
            return classinfoMapper.getClassInfoListLength(search);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteClassinfoById(int id) {
        try{
            List<Student> students = studentMapper.getStudentListByClassinfoId(id);
            int count = students.size();

            for(int i = 0;i<students.size();i++){
                int rows = studentMapper.deleteByPrimaryKey(students.get(i).getId());
            }

            Classinfo classinfo = classinfoMapper.selectByPrimaryKey(id);
            classinfo.setStudentcount(classinfo.getStudentcount() - count);

            Project project = projectMapper.selectByPrimaryKey(classinfo.getProjectid());
            project.setStudentcount(project.getStudentcount() - count);

            int rows = classinfoMapper.deleteByPrimaryKey(id);
            if(rows<0){
                return -1;
            }

            rows = projectMapper.updateByPrimaryKey(project);
            if(rows<0){
                return -1;
            }else{
                return rows;
            }

        }catch (Exception e){

        }
        return 0;
    }

    @Override
    public int getMaxId() {
        return classinfoMapper.getMaxId();
    }

    @Override
    public int addClassInfoByEntity(Classinfo classinfo) {
        try{
            int rows = classinfoMapper.selectByclassName(classinfo.getClassname());
            if(rows == 0) {
                return classinfoMapper.insertSelective(classinfo);
            }else {
                return 0;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Classinfo getClassInfosByPrimaryKey(Integer id) {
        try{
            return classinfoMapper.selectByPrimaryKey(id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateClassinfoHandle(Classinfo oriClassInfo, Classinfo classinfo) {
        try{
            if(oriClassInfo.getProjectid() != classinfo.getProjectid()){
              Project oriProject = projectMapper.selectByPrimaryKey(oriClassInfo.getProjectid());
              oriProject.setStudentcount(oriProject.getStudentcount() - oriClassInfo.getStudentcount());

              Project newProject = projectMapper.selectByPrimaryKey(classinfo.getProjectid());
              newProject.setStudentcount(newProject.getStudentcount() - classinfo.getStudentcount());
              int rows = projectMapper.updateByPrimaryKey(oriProject);
              if(rows <= 0){
                  return rows;
              }

              rows = projectMapper.updateByPrimaryKey(newProject);
              return rows;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateClassInfoByEntity(Classinfo classinfo) {
        try{
            return classinfoMapper.updateByPrimaryKey(classinfo);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int ClassNameIsExist(String classname) {
        try{
            return classinfoMapper.ClassNameIsExist(classname);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int addClassInfosByExcelFile(String uploadCurrentFileName) {
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            DecimalFormat decimalFormat = new DecimalFormat("##0");//格式化设置
            InputStream is = new FileInputStream(uploadCurrentFileName);
            Workbook wb = null;
            int realAddPersonCount = 0;
            if(uploadCurrentFileName.split("\\.")[1].equals("xls")){
                wb = new HSSFWorkbook(is);
            }else if(uploadCurrentFileName.split("\\.")[1].equals("xlsx")){
                wb = new XSSFWorkbook(is);
            }

            int sheetNumber = wb.getNumberOfSheets();
            int InsertCount = 0;
            Row title = wb.getSheetAt(0).getRow(0);
            if(!title.getCell(0).getStringCellValue().equals("班级名称")||
                    !title.getCell(1).getStringCellValue().equals("专业编号")||
                    !title.getCell(2).getStringCellValue().equals("专业名称")||
                    !title.getCell(3).getStringCellValue().equals("年级")){
                return 0;
            }


            for(int i = 0;i<sheetNumber;i++) {
                int index = 1;
                for(Row row : wb.getSheetAt(i)){
                    if(row == null){
                        continue;
                    }else{
                        if(index == 1){

                        }else{
                            if(row.getCell(0) == null || row.getCell(1) == null){
                                index++;
                                continue;
                            }else{
                                Classinfo classinfo = new Classinfo();
                                classinfo.setId(classinfoMapper.getMaxId() + 1);
                                classinfo.setStudentcount(0);
                                classinfo.setClassname(Util.formGetCellType(row.getCell(0)));
                                classinfo.setProjectid(Integer.parseInt(Util.formGetCellType(row.getCell(1))));
                                String gradeInfo = Util.formGetCellType(row.getCell(3));
                                Integer gradeId = gradeMapper.getGradeIdByGradeInfo(gradeInfo);
                                classinfo.setGrade(gradeId);
                                classinfoMapper.insertSelective(classinfo);
                                InsertCount = InsertCount + 1;
                            }
                        }
                        index++;
                    }
                }
            }


            return InsertCount;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<PersonArriveInfoNameAndMax> getAllClassStudentArrivedInfo(Integer classid) {
        try{
            return classinfoMapper.getAllClassStudentArrivedInfo(classid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Float> getAllClassStudentArrivedInfoValues(Integer classid) {
        try{
            return classinfoMapper.getAllClassStudentArrivedInfoValues(classid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
